package com.xilihui.xlh.core.util;

import android.text.TextUtils;


import com.xilihui.xlh.constant.StrConst;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 2017/2/10.
 */

public class SignUtil {


    public static String sign(Map<String, String> map) {

        StringBuilder stringBuilder = new StringBuilder();
        if (map != null) {
            Collection<String> keySet = map.keySet();
            List<String> list = new ArrayList<>(keySet);
            // key按首字母排序
            Collections.sort(list); 
            for (int i = 0; i < list.size(); i++) {

                if (TextUtils.isEmpty(list.get(i))) {
                    // key是空值过滤
                    continue;
                }

                if (TextUtils.isEmpty(map.get(list.get(i)))) {
                    // value是空值过滤
                    continue;
                }
                stringBuilder.append(list.get(i))
                        .append(map.get(list.get(i)));

            }
        }
        String secret = StrConst.SECRET;
        stringBuilder.append(secret);
        if (!TextUtils.isEmpty(stringBuilder.toString())) {
            // sha1加密
            String sha1 = EncryptUtil.encryptSHA1(stringBuilder.toString());
//            LogUtil.e("Sign String", stringBuilder.toString() + "\nsha1: " + sha1);
            return sha1;
        }
        return null;
    }
}
